package vn.vfriends.vbox.storage;

import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
import vn.vfriends.vbox.facade.AccountFacade;
import vn.vfriends.vbox.model.Storage;
import vn.vfriends.vbox.model.Account;
import vn.vfriends.vbox.model.File;
import vn.vfriends.vbox.rest.reader.ListFileReader;

/**
 *
 * @author tuancn
 */
@Stateless
public class GoogleDriveService {
    
    private static final String API_KEY = "609067126048-8b2foeprfn9bmlfrjouaa69n6u2etp2a.apps.googleusercontent.com";
    
    private static final String API_SECRET = "Spqi7tBC5rf6vAexe3GmjkWM";
    
    @Inject
    private AccountFacade accountFacade;
    
    public void refreshToken(Account account) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://accounts.google.com/o/oauth2/token");
        
        Form form = new Form();
        form.param("refresh_token", account.getRefreshToken());
        form.param("client_id", API_KEY);
        form.param("client_secret", API_SECRET);
        form.param("grant_type", "refresh_token");
        
        GoogleTokenResponse res = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), GoogleTokenResponse.class);
        
        String accessToken = res.getAccess_token();
        account.setAccessToken(accessToken);
        
        accountFacade.edit(account);
    }
    
    public Storage getStorage(String token) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://www.googleapis.com/drive/v2/about").queryParam("access_token", token);
        return target.request().get(Storage.class);
    }
    
    public List<File> getFiles(String accessToken, String fileId) {
        fileId = (fileId.equals("/")) ? "root" : fileId;
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://www.googleapis.com/drive/v2/files")
                .queryParam("access_token", accessToken)
                .queryParam("q", "'" + fileId + "' in parents and trashed = false");
        
        target.register(ListFileReader.class);
        
        return target.request(MediaType.APPLICATION_JSON).get(new GenericType<List<File>>() {});
    }
}

@XmlRootElement
class GoogleTokenResponse {

    private String access_token;

    private long expires_in;

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public long getExpires_in() {
        return expires_in;
    }

    public void setExpires_in(long expires_in) {
        this.expires_in = expires_in;
    }
}